#import "VLayoutView.h"

@implementation VLayoutView

- (id)initWithFrame:(CGRect)frame spacing:(int)s leftMargin:(int)lm
    rightMargin:(int)rm topMargin:(int)tm bottomMargin:(int)bm
    alignment:(UIControlContentVerticalAlignment)a
{
    if (self = [super initWithFrame:frame spacing:s leftMargin:lm rightMargin:rm
                topMargin:tm bottomMargin:bm])
    {
        if (a == UIControlContentVerticalAlignmentFill)
            a = UIControlContentVerticalAlignmentCenter;
        alignment = a;
    }
    return self;
}

- (CGSize)layoutSubviewsEffectively:(BOOL)effectively
{
    switch (alignment)
    {
        case UIControlContentVerticalAlignmentTop:
        {
            int left = leftMargin, top = topMargin, max_right = 0;
            for (int i = 0; i < [self.subviews count]; ++i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != 0)
                    top += spacing;
                if (effectively)
                    child.frame = CGRectMake(left, top, child.frame.size.width,
                                             child.frame.size.height);
                top += child.frame.size.height;
                if (max_right < left + child.frame.size.width)
                    max_right = left + child.frame.size.width;
            }
            return CGSizeMake(max_right + rightMargin, top + bottomMargin);
        }
        case UIControlContentVerticalAlignmentBottom:
        {            
            int left = leftMargin, bottom = self.frame.size.height
                - bottomMargin, max_right = 0;
            for (int i = [self.subviews count] - 1; i >= 0; --i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != [self.subviews count] - 1)
                    bottom -= spacing;
                if (effectively)
                    child.frame = CGRectMake(left,
                                             bottom - child.frame.size.height,
                                             child.frame.size.width,
                                             child.frame.size.height);
                bottom -= child.frame.size.height;
                if (max_right < left + child.frame.size.width)
                    max_right = left + child.frame.size.width;
            }
            return CGSizeMake(max_right + rightMargin,
                              self.frame.size.height - bottom + topMargin);
        }
        default: // center
        {
            int total_height = topMargin;
            for (UIView *child in self.subviews)
                total_height += child.frame.size.height;
            total_height += ([self.subviews count] - 1) * spacing
                + bottomMargin;
            int left = leftMargin, top = self.frame.size.height / 2
                - total_height / 2 + leftMargin, max_right = 0;
            for (int i = 0; i < [self.subviews count]; ++i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != 0)
                    top += spacing;
                if (effectively)
                    child.frame = CGRectMake(left, top, child.frame.size.width,
                                             child.frame.size.height);
                top += child.frame.size.height;
                if (max_right < left + child.frame.size.width)
                    max_right = left + child.frame.size.width;
            }
            return CGSizeMake(max_right + rightMargin, total_height);
        }
    }
}

@end
